<template>
	<view class="all-main">
		<view class="main">
			<!-- 总交易额 -->
			<view class="detailed_card">
				<view class="titles">
					<text>激活数据统计分析</text>
				</view>
				<view class="detailed_card_title">
					<u-icon size="18" color="#ffffff" name="hourglass-half-fill"></u-icon>
					<text class="card">总激活(台)</text>
				</view>
				<h2>{{totalData.allTotal}}</h2>
			</view>
			<!-- 筛选 -->
			<view class="search">
				<view class="month" @click="monthShow = true">
					<u-icon name="calendar" color="#2979ff" size="28"></u-icon>
					<view class="values">
						<text>{{month}}</text>
					</view>
				</view>
				<view class="product" @click="proShow = true">
					<u-icon name="list-dot" color="#ff9900" size="28"></u-icon>
					<view class="values">
						<text>{{product_name}}</text>
					</view>
				</view>
			</view>
			<!-- 统计列表 -->
			<view class="lists">
				<view v-for="(item, index) in totalData.lists" :key="index" class="items">
					<view class="level-one">
						<view class="agents">
							{{item.name}}
						</view>
						<view class="totals" @click="oneClick(item)">
							<text>{{item.total}}</text>
							<u-icon v-if="!item.show" name="../../../../static/icons/double-arrows-down.png" color="#2979ff"
								size="15"></u-icon>
							<u-icon v-else name="../../../../static/icons/double-arrows-up.png" color="#2979ff"
								size="15"></u-icon>
						</view>
					</view>
					<view v-show="item.show" class="level-two">
						<view v-for="(items, indexs) in item.lists" :key="indexs" class="two-item" :class="items.show ? 'active': ''">
							<view class="two-list">
								<view class="months">
									<text>{{items.month}}</text>
								</view>
								<view class="month-total" @click="twoClick(items)">
									<text>{{items.total}}</text>
									<u-icon v-if="!items.show" name="arrow-down" color="#2979ff" bold size="15"></u-icon>
									<u-icon v-else name="arrow-up" color="#909399" bold size="15"></u-icon>
								</view>
							</view>
							<view v-if="items.show" class="two-detai">
								<view v-for="(itemt, indext) in items.lists" :key="indext" class="detai-item" @click="detail(itemt, item.id)">
									<view class="day">
										<text>{{itemt.day}}</text>
									</view>
									<view class="day-total">
										<text>{{itemt.total}}</text>
									</view>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
			<!-- 日期选择 -->
			<u-datetime-picker :show="monthShow" v-model="value1" :minDate="dates.minDate" :maxDate="dates.maxDate"
				mode="year-month" @confirm="monthConfirm" @cancel="monthShow = false" @close="monthShow = false"
				:closeOnClickOverlay="true"></u-datetime-picker>
			<!-- 产品选择 -->
			<u-picker :show="proShow" ref="uPicker" :columns="proList" keyName="title" closeOnClickOverlay
				@confirm="productSelect" @cancel="proShow = false" @close="proShow = false"></u-picker>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				dates: {},
				value1: '',
				monthShow: false,
				oneState: false,
				twoState: false,
				month: '2025-09',
				proShow: false,
				product_id: '',
				product_name: '全部产品',
				proList: [],
				userinfo: {},
				totalData: {
					allTotal: "0.00",
					lists: []
				},
			}
		},
		onLoad() {
			this.userinfo = uni.getStorageSync('userinfo')
			this.getHzyDateSetting()
			this.getHzyProduct()
		},
		methods: {
			// 获取日期配置
			async getHzyDateSetting() {
				uni.showLoading({
					title: '加载中'
				});
				const {
					data: res
				} = await this.$myHttp({
					url: 'v1/getHzyDateSetting'
				});
				uni.hideLoading()
				if (res.code == 200) {
					this.month = res.data.month
					this.dates = res.data
					this.loadData()
				}
			},
			// 获取产品
			async getHzyProduct() {
				uni.showLoading({
					title: '加载中'
				});
				const {
					data: res
				} = await this.$myHttp({
					url: 'v1/getHzyTotalProduct'
				});
				uni.hideLoading()
				if (res.code == 200) {
					this.proList = [res.data]
				}
			},
			// 加载数据
			async loadData() {
				uni.showLoading({
					title: '加载中'
				});
				const {
					data: res
				} = await this.$myHttp({
					url: 'v1/getHzyActiveDataTotal',
					data: {
						month: this.month,
						product_id: this.product_id
					}
				});
				uni.hideLoading()
				if (res.code == 200) {
					this.totalData = res.data
				}
			},
			// 一级展开
			oneClick(item) {
				if (item.lists.length > 0) {
					item.show = !item.show
				}
			},
			// 二级展开
			twoClick(items) {
				if (items.lists.length > 0) {
					items.show = !items.show
				}
			},
			// 日期选择
			monthConfirm(e) {
				const timeFormat = uni.$u.timeFormat
				this.month = timeFormat(e.value, 'yyyy-mm')
				this.monthShow = false
				this.loadData()
			},
			// 产品选择
			productSelect(e) {
				if (this.product_id != e.value[0].id) {
					this.product_id = e.value[0].id
					this.product_name = e.value[0].title
					this.proShow = false
					this.loadData()
				}
			},
			// 详情
			detail(item, agent_id) {
				if (agent_id == this.userinfo.id) {
					uni.navigateTo({
						url: '/pages/hzy/data/active/detail?day=' + item.day + '&product_id=' + this.product_id
					})
				}
			}
		}
	}
</script>

<style lang="scss">
	.all-main {
		.main {
			padding: 30rpx;

			.detailed_card {
				padding: 30rpx;
				border-radius: 15rpx;
				box-shadow: 7px 0px 30px -18px #888888;
				margin-bottom: 30rpx;
				display: flex;
				flex-direction: column;
				align-items: center;
				background: url('@/static/total_bgimg.png') no-repeat center;

				.detailed_card_title {
					padding-top: 20rpx;
					display: flex;
				}

				.card {
					font-size: 28rpx;
					color: #fff;
					padding-left: 8rpx;
				}

				h2 {
					font-weight: 600;
					font-size: 42rpx;
					margin-top: 20rpx;
					color: #fff;
				}

				.titles {
					font-size: 50rpx;
					font-weight: bold;
					color: #fff;
				}
			}

			.search {
				margin-top: 20rpx;
				display: flex;
				justify-content: space-between;
				align-items: center;
				font-size: 30rpx;

				.month {
					display: flex;
					color: #2979ff;
					font-weight: bold;
					align-items: center;
				}

				.product {
					display: flex;
					color: #ff9900;
					font-weight: bold;
					align-items: center;
				}

				text {
					padding-left: 8rpx;
					padding-top: 5rpx;
				}
			}

			.lists {
				margin-top: 30rpx;
				font-size: 28rpx;

				.items {
					padding: 30rpx;
					background-color: #fff;
					box-shadow: 7px 0px 30px -18px #888888;
					border-radius: 8rpx;
					margin-bottom: 30rpx;

					.level-one {
						display: flex;
						justify-content: space-between;
						align-items: center;
						font-weight: bold;

						.totals {
							display: flex;

							text {
								padding-right: 20rpx;
							}
						}
					}

					.level-two {
						margin-top: 30rpx;
						border-top: 1px solid #909399;
						animation: slideIn 1s;

						.two-item {
							padding: 30rpx 0;
							border-bottom: 1px dashed #c8c9cc;

							.two-list {
								display: flex;
								justify-content: space-between;
								align-items: center;

								.month-total {
									display: flex;

									text {
										padding-right: 20rpx;
									}
								}
							}

							.two-detai {
								margin-top: 30rpx;
								font-size: 24rpx;
								color: #909399;
								border: 1px dashed #f4f4f5;
								border-radius: 10rpx;

								.detai-item {
									padding: 30rpx;
									display: flex;
									justify-content: space-between;
									align-items: center;
									border-bottom: 1px dashed #f4f4f5;
								}
							}
						}
						
						.active {
							color: #2979ff;
							font-weight: bold;
						}
					}
				}
			}
		
			/* 移动动画 */
			@keyframes slideIn {
				0% { opacity: 0; }
			    100% { opacity: 1; }
			}
		}
	}
</style>